﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KTOP.Core.Domain.Matrix;
using KTOP.Core.Extensions;

namespace KTOP.Homeworks.Hw4.Logics.MaxThroughputRoute
{
    internal class FrankFricheHistoryElement
    {
        public int[][] Matrix { get; set; }
        public List<int> GatheredNodes { get; set; }
        public int Q { get; set; }
    }

    class MaxThroughputRouteBuilder
    {
        private static int[][] CopyMatrix(IEnumerable<IEnumerable<int>> matrix)
        {
            return matrix.Select(row => row.ToArray()).ToArray();
        }

        public static void FrankFricheIteration(int start, int end, CMatrix cMatrix)
        {
            var history = new List<FrankFricheHistoryElement>();

            var matrix = CopyMatrix(cMatrix.Matrix);
            var gathered = new List<int>() {start};

            while (!gathered.Contains(end))
            {
        
                var q = matrix
                    .Where((_, i) => gathered.Contains(i))
                    .SelectMany(r => r)
                    .Where((e,i) => e > 0 )
                    .Max();

                for (var row = 0; row < matrix.Length; row++)
                    for (var col = row+1; col < matrix.Length; col++)
                        if (matrix[row][col] >= q)
                        {
                            gathered.Add(col);
                            gathered.Add(row);

                            matrix[row][col] = -1;
                            matrix[col][row] = -1;

                        }

                //vmap = vmap.Select(map => map.Distinct().ToList()).ToArray();
                history.Add(new FrankFricheHistoryElement()
                                {
                                    Matrix = CopyMatrix(matrix),
                                    GatheredNodes = gathered.Distinct().ToList(),
                                    Q = q
                                });
            }
        }



    }
}
